class AttendeesController < ApplicationController

  require 'open-uri'
  require 'prawn/measurement_extensions'
	
  pdf_img = "#{RAILS_ROOT}/public/images/sulpet_pdf.jpg"

  prawnto :prawn =>{ :page_size => 'A4', 
					 :page_layout => :landscape,
					 :background => pdf_img,
					 :left_margin => 5.cm,    
                     :right_margin => 5.cm,    
                     :top_margin => 20.cm,    
                     :bottom_margin => 10.cm   
					}

  before_filter :check_logged, :except => [ :index ]
  before_filter :get_attendee, :only => [ :edit, :update, :show ]
  before_filter :check_deadline, :only => [ :new, :destroy]

  def check_deadline
    if false#true
      flash[:notice] = "As inscrições estão fechadas"
      redirect_to attendees_path
    end
  end

  def options str
  	{ 'pucrs' => { :ies => 'PUCRS' },
      'ufrgs' => { :ies => 'UFRGS'},
      'pagos' => {:paid => true},
      'npagos' => {:paid => false },
      'alojamento' => { :lodge => true },
      'nalojamento' => { :lodge => false },
      'fila'	=>	{:queue => true},
      'nfila'	=>  {:queue => false},
      'tutor'	=>  {:tutor => true},
      'ntutor'	=>  {:tutor => false},
      'cancelado' => {:canceled => true},
      'ncancelado' => {:canceled => false}
    }[str] || {}
  end

  private :options

  # GET /attendees
  # GET /attendees.xml
  def index

    @attendee = Attendee.find(:all, :conditions => {:user_login => current_user.login} ) if logged_in?

    if authorized?

	  conditions = {}

	  #adding all conditions that match "mostrar(something)" to the query
	  params.each do |k,v|
		 conditions.merge! options(v) if /^mostrar/.match(k)
	  end

	  #not using pagination anymore...
      #@attendees = Attendee.paginate :all, :per_page => per_page, :conditions => conditions, :page => params[:page], :order => 'created_at DESC'

      @attendees = Attendee.find :all, :conditions => conditions, :order => 'created_at DESC'
	  @pretty_view = params['bonito'] == 'sim'

	  @attendees = @attendees.sort_by { |a| a.name.upcase } if @pretty_view

    else
      @attendees = @attendee
    end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @attendees }
    end
  end

  # GET /attendees/1
  # GET /attendees/1.xml
  def show
    #@attendee = Attendee.find(params[:id])

    respond_to do |format|
	  format.pdf { render :pdf => @attendee }
      format.html # show.html.erb
    end
  end

  # GET /attendees/new
  # GET /attendees/new.xml
  def new
    @attendee = Attendee.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @attendee }
    end

  end

  # GET /attendees/1/edit
  def edit

  end

  # POST /attendees
  # POST /attendees.xml
  def create
    @attendee = Attendee.new(params[:attendee])

    respond_to do |format|
      if @attendee.save
      	Attendee.expire_cache
        #Attendee.give_lodge_to_first_attendee
        flash[:notice] = 'Inscrição Realizada com Sucesso'
        format.html { redirect_to(attendees_path) }
        format.xml  { render :xml => @attendee, :status => :created, :location => @attendee }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @attendee.errors, :status => :unprocessable_entity }
      end
    end

  end

  # PUT /attendees/1
  # PUT /attendees/1.xml
  def update

    #@attendee = Attendee.find(params[:id])

    respond_to do |format|
      if @attendee.update_attributes(params[:attendee])
        Attendee.expire_cache
        #Attendee.give_lodge_to_first_attendee
        flash[:notice] = 'Dados Atualizados com Sucesso'
        format.html { redirect_to(attendees_path) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @attendee.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /attendees/1
  # DELETE /attendees/1.xml
  def destroy
    @attendee = Attendee.find(params[:id])
    @attendee.destroy

    Attendee.expire_cache
    #Attendee.give_lodge_to_first_attendee

    respond_to do |format|
      format.html { redirect_to(attendees_url) }
      format.xml  { head :ok }
    end
  end

  def get_attendee
    if authorized?
      @attendee = Attendee.find(params[:id])
    elsif logged_in?
      @attendee = Attendee.find(:first, :conditions => {:user_login => current_user.login} )
    end

    #store the old gd and lodge status of the attendee
    @attendee.old_gd = @attendee.gd
    @attendee.old_lodge = @attendee.lodge
  end

  #private :show, :new, :create, :edit, :destroy, :update
end
